#include "CCmdInnerCreateUser.h"
#include "../mainctrl.h"
#include "../cmd/cmdapi.h"
#include "commmsg.h"
#include "../cmdfront/cmduserinfo.h"
#include "MsgLogin.pb.h"
#include "MsgUserInfo.pb.h"

CT_REGISTER_ASYNCCMD(ID_MAIN_INNER_CREATE_USER, CCmdInnerCreateUser);

int CCmdInnerCreateUser::Do(void* pData)
{
    if (pData != NULL)
    {
        CT_ERROR(("inner cmd CCmdInnerCreateUser get data"));
        return AnsClients(RET_SYS_ERROR);
    }

    if (pstMainCtrl == NULL)
    {
        CT_ERROR(("mainctrl point is null"));
        return AnsClients(RET_SYS_ERROR);
    }

    //先判断是否已经有角色
    if (strlen(_stPara.GetValue("openid")) > 0)
    {
        _szUserToken.Set("%s", _stPara.GetValue("openid"));
    }
    else if (0 == strcmp(_stPara.GetValue("platform"), "32wan"))
    {
        _szUserToken.Set("%s", _stPara.GetValue("username"));
    }
    else if (0 == strcmp(_stPara.GetValue("platform"), "yd10086"))
    {
        _szUserToken.Set("%s", _stPara.GetValue("username"));
    }
    else
    {
#ifdef _DEBUG
        _szUserToken.Set("%s", _stPara.GetValue("usertoken"));
#else
        CT_ERROR((""));
        return AnsClients(RET_SYS_ERROR);
#endif
    }

    if (_szUserToken.GetDataLen() <= 0)
    {
        CT_ERROR((""));
        return AnsClients(RET_SYS_ERROR);
    }

    int iIndex = pstMainCtrl->m_stUserTokenToGID.HashFind(_szUserToken);
    if (iIndex >= 0)
    {
        CT_ERROR((""));
        return AnsClients(RET_SYS_ERROR);
    }

    if (strlen(_stPara.GetValue("platform")) <= 0)
    {
        CT_ERROR((""));
        return AnsClients(RET_SYS_ERROR);
    }

    if (pstMainCtrl->IsTencentPlatform(_stPara.GetValue("platform")))
    {
        if (strlen(_stPara.GetValue("openid")) <= 0 ||
            strlen(_stPara.GetValue("openkey")) <= 0 ||
            strlen(_stPara.GetValue("pf")) <= 0 ||
            strlen(_stPara.GetValue("seqid")) <= 0 ||
            strlen(_stPara.GetValue("pfkey")) <= 0)
        {
            CT_ERROR((""));
            return AnsClients(RET_SYS_ERROR);
        }
    }

    if (strlen(_stPara.GetValue("ismale")) <= 0 ||
        strlen(_stPara.GetValue("career")) <= 0 ||
        strlen(_stPara.GetValue("rolename")) <= 0)
    {
        CT_ERROR((""));
        return AnsClients(RET_SYS_ERROR);
    }

    if (atoi(_stPara.GetValue("career")) < CAREER_BEGIN || atoi(_stPara.GetValue("career")) > CAREER_END)
    {
        CT_ERROR((""));
        return AnsClients(ERROR_WRONG_CAREER);
    }

    LogicDo(RET_OK);
    return RET_OK;
}

int CCmdInnerCreateUser::LogicDo(int iRet)
{
    if (iRet != RET_OK)
    {
        CT_ERROR(("CCmdInnerCreateUser error , ret %d", iRet));
        return AnsClients(RET_SYS_ERROR);
    }

    if (!_bHasCreateLogin)
    {
        _bHasCreateLogin = true;

        MsgReqLogin stLoginReq;
        stLoginReq.set_user_token(_stPara.GetValue("usertoken"));

        stLoginReq.set_open_id(_stPara.GetValue("openid"));
        stLoginReq.set_open_key(_stPara.GetValue("openkey"));
        stLoginReq.set_pf(_stPara.GetValue("pf"));
        stLoginReq.set_seqid(_stPara.GetValue("seqid"));
        stLoginReq.set_pfkey(_stPara.GetValue("pfkey"));

        stLoginReq.set_invkey(_stPara.GetValue("invkey"));
        stLoginReq.set_iopenid(_stPara.GetValue("iopenid"));
        stLoginReq.set_itime(_stPara.GetValue("itime"));
        stLoginReq.set_platform(_stPara.GetValue("platform"));

        stLoginReq.set_params(_szHttpStr.Get());

        CCmdLogin* pstCmd = (CCmdLogin*)CT_CMDFACTORY->CreateCmd(ID_MAIN_LOGIN);
        if (!pstCmd)
        {
            CT_ERROR(("create cmd CCmdLogin error"));
            return AnsClients(RET_SYS_ERROR);
        }

        pstCmd->bIsHttpCreateUser = true;
        pstCmd->szRoleName.Set("%s", CTStringUtils::UrlDecode(_stPara.GetValue("rolename")));

        PBMsgHead stMsgHead;
        stMsgHead.set_version(0);
        stMsgHead.set_time(pstMainCtrl->m_iNow);
        stMsgHead.set_msgact(0);
        stMsgHead.set_msgid(ID_MAIN_LOGIN);
        stMsgHead.set_msgret(0);
        stMsgHead.set_msgtype(CSMsgDef::EMT_REQUEST);
        
        pstMainCtrl->m_stEncodeBuf.Clear();
        iRet = pstMainCtrl->EncodePBMsg(stLoginReq, pstMainCtrl->m_stEncodeBuf, false);
        if (iRet)
        {
            CT_ERROR((""));
            return AnsClients(iRet);
        }
        
        CTCodeStream s;
        iRet = s.Attach(pstMainCtrl->m_stEncodeBuf.GetUsedBuf(), pstMainCtrl->m_stEncodeBuf.GetUsedLen());
        if (iRet)
        {
            CT_ERROR((""));
            return AnsClients(iRet);
        }
        
        CCmdPBDoParam stParam((void*)pstMainCtrl, stEnpHead, stMsgHead, s);

        RegisterChild(pstCmd, true);
        return pstCmd->Do(&stParam);
    }
    else
    {
        if (m_pstChild && ID_MAIN_LOGIN == m_pstChild->GetCmdID())
        {
            CCmdLogin* pstCmd = (CCmdLogin*)m_pstChild;
            if (pstCmd->uiGID <= 0)
            {
                CT_ERROR((""));
                return AnsClients(RET_SYS_ERROR);
            }
            else
            {
                _uiGID = pstCmd->uiGID;
            }
        }
    }

    if (!_bHasInitUser && _uiGID > 0)
    {
        _bHasInitUser = true;

        MsgReqInitUserInfo stInitReg;
        stInitReg.set_gid(_uiGID);
        stInitReg.set_is_male(atoi(_stPara.GetValue("ismale")));
        stInitReg.set_career(atoi(_stPara.GetValue("career")));
        stInitReg.set_role_name(CTStringUtils::UrlDecode(_stPara.GetValue("rolename")));

        CCmdInitUserInfo* pstCmd = (CCmdInitUserInfo*)CT_CMDFACTORY->CreateCmd(ID_MAIN_INIT_USER_INFO);
        if (!pstCmd)
        {
            CT_ERROR(("create cmd CCmdInitUserInfo error"));
            return AnsClients(RET_SYS_ERROR);
        }

        pstCmd->_bIsHttpInit = true;
        pstCmd->_bIsTencentLogin = pstMainCtrl->IsTencentPlatform(_stPara.GetValue("platform"));

        PBMsgHead stMsgHead;
        stMsgHead.set_version(0);
        stMsgHead.set_time(pstMainCtrl->m_iNow);
        stMsgHead.set_msgact(0);
        stMsgHead.set_msgid(ID_MAIN_INIT_USER_INFO);
        stMsgHead.set_msgret(0);
        stMsgHead.set_msgtype(CSMsgDef::EMT_REQUEST);
        
        pstMainCtrl->m_stEncodeBuf.Clear();
        iRet = pstMainCtrl->EncodePBMsg(stInitReg, pstMainCtrl->m_stEncodeBuf, false);
        if (iRet)
        {
            CT_ERROR((""));
            return AnsClients(iRet);
        }
        
        CTCodeStream s;
        iRet = s.Attach(pstMainCtrl->m_stEncodeBuf.GetUsedBuf(), pstMainCtrl->m_stEncodeBuf.GetUsedLen());
        if (iRet)
        {
            CT_ERROR((""));
            return AnsClients(iRet);
        }
        
        CCmdPBDoParam stParam((void*)pstMainCtrl, stEnpHead, stMsgHead, s);

        RegisterChild(pstCmd, true);
        return pstCmd->Do(&stParam);
    }
    else
    {
        if (m_pstChild && ID_MAIN_INIT_USER_INFO == m_pstChild->GetCmdID())
        {
            CCmdInitUserInfo* pstCmd = (CCmdInitUserInfo*)m_pstChild;
            return AnsClients(pstCmd->_iRet);
        }
    }

    CT_ERROR((""));
    return AnsClients(RET_SYS_ERROR);
}

int CCmdInnerCreateUser::AnsClients(int iRet)
{
    pstMainCtrl->m_stEncodeBuf.Clear();
    CTSizeString<64> szTmp;
    CTSizeString<1024> szTmp2;

    if (iRet)
    {
        szTmp.Set("%d", iRet);
        szTmp2.Set("HTTP/1.1 200 OK\r\nContent-length:%d\r\n\r\n%s\r\n",
            (szTmp.GetDataLen() + 2), szTmp());
    }
    else
    {
        szTmp2.Set("HTTP/1.1 200 OK\r\nContent-length:3\r\n\r\n0\r\n");
    }

    CT_TRACE(("%s", szTmp2.Get()));
    
    memcpy(pstMainCtrl->m_stEncodeBuf.GetFreeBuf(), szTmp2(),
        szTmp2.GetDataLen());

    pstMainCtrl->m_stEncodeBuf.Append(szTmp2.GetDataLen());

    if (pstMainCtrl->AnswerToClient(stEnpHead, pstMainCtrl->m_stEncodeBuf, true))
    {
        return Done(RET_SYS_ERROR);
    }

    return Done(iRet);
}

